package com.zhn.evsi.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhn.evsi.client.AllianceClient;
import com.zhn.evsi.client.CharacterClient;
import com.zhn.evsi.client.CloneClient;
import com.zhn.evsi.client.CorporationClient;
import com.zhn.evsi.domain.dto.evsi.*;
import com.zhn.evsi.domain.dto.evsi.alliance.EvsiAllianceInfoDTO;
import com.zhn.evsi.domain.dto.evsi.character.*;
import com.zhn.evsi.domain.dto.evsi.clone.EvsiCloneDTO;
import com.zhn.evsi.domain.dto.evsi.common.EvsiIconDTO;
import com.zhn.evsi.domain.dto.evsi.corporation.EvsiCorporationPublicInfoDTO;
import com.zhn.evsi.domain.entity.System.SysFile;
import com.zhn.evsi.domain.entity.System.SysUser;
import com.zhn.evsi.domain.entity.evsi.EvsiCharacter;
import com.zhn.evsi.domain.entity.evsi.EvsiInvtypes;
import com.zhn.evsi.exception.CustomException;
import com.zhn.evsi.interceptor.CharacterInterceptor;
import com.zhn.evsi.mapper.EvsiCharacterMapper;
import com.zhn.evsi.service.IEvsiCharacterService;
import com.zhn.evsi.service.IEvsiInvtypesService;
import com.zhn.evsi.service.ISysFileService;
import com.zhn.evsi.utils.MultipartFileUtil;
import com.zhn.evsi.utils.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class EvsiCharacterServiceImpl extends ServiceImpl<EvsiCharacterMapper, EvsiCharacter> implements IEvsiCharacterService {

    @Autowired
    private OSSUtil ossUtil;
    @Autowired
    private MultipartFileUtil multipartFileUtil;
    @Autowired
    private CloneClient cloneClient;
    @Autowired
    private CharacterClient characterClient;
    @Autowired
    private CorporationClient corporationClient;

    @Autowired
    private IEvsiInvtypesService invtypesService;
    @Autowired
    private ISysFileService fileService;
    @Autowired
    private CharacterInterceptor characterInterceptor;

    /**
     * 获取角色公开信息
     * */
    @Override
    public EvsiCharacterPublicInfoDTO publicInfo() {
        return characterInterceptor.getCharacters(null);
    }

    /**
     * 获取当前地址
     * */
    @Override
    public EvsiCharacterLocationSolarDTO location() {
        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);
        if (ObjectUtil.isNull(characters)) {
            throw new CustomException("角色id不可为空，请重新授权角色信息");
        }
        EvsiCharacterLocationDTO location = characterClient.location(characters.getCharacters());
        EvsiCharacterLocationSolarDTO evsiCharacterLocationSolarDTOS = this.baseMapper.solarSystemId(location.getSolar_system_id());
        evsiCharacterLocationSolarDTOS.setStructureId(location.getStructure_id());
        return evsiCharacterLocationSolarDTOS;
    }

    /**
     * 获取当前登录状态
     * */
    @Override
    public EvsiCharacterOnlineDTO online() {
        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);
        if (ObjectUtil.isNull(characters)) {
            throw new CustomException("角色id不可为空，请重新授权角色信息");
        }
        return characterClient.online(characters.getCharacters());
    }

    /**
     * 获得当前舰船
     * */
    @Override
    public EvsiCharacterShipDTO ship() {
        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);
        if (ObjectUtil.isNull(characters)) {
            throw new CustomException("角色id不可为空，请重新授权角色信息");
        }
        EvsiCharacterShipDTO ship = characterClient.ship(characters.getCharacters());
        String shipName = this.baseMapper.getShipInfo(ship.getShip_type_id());
        ship.setTypeName(shipName);
        // TODO 从oss中获取舰船图片
        File file = new File("H:\\production\\types\\Invasion_1.0_Types\\Types\\" + ship.getShip_type_id() + "_64.png");
        String shipPic = uploadFile(file);
        ship.setPicUrl(shipPic);
        return ship;
    }

    /**
     * OSS文件上传
     * */
    private String uploadFile(File file){
        MultipartFile multipartFile = multipartFileUtil.getMultipartFile(file);
        String fileMd5 = null;
        try {
            InputStream inputStream = multipartFile.getInputStream();
            fileMd5 = DigestUtil.md5Hex(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        SysFile sysFile = fileService.getOne(new LambdaQueryWrapper<SysFile>().eq(SysFile::getFileMd5, fileMd5).eq(SysFile::getState, 1));
        // TODO SecurityUtil.getUsername;
        String username = "1085155177@qq.com";
        if (ObjectUtil.isNull(sysFile)) {
            String avatar = ossUtil.OSSUpload(multipartFile, "ship", 1, username);
            return avatar;
        } else {
            return sysFile.getFilePath();
        }
    }

    /**
     * 获取克隆体
     * */
    @Override
    public EvsiCloneDTO clones() {
        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);
        return cloneClient.clones(characters.getCharacters());
    }

    /**
     * 获取植入体
     * */
    @Override
    public List<JSONObject> implants() {
        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);
        return cloneClient.implants(characters.getCharacters());
    }

    /**
     * 研究
     * */
    @Override
    public Object agentsResearch() {
        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);
        return characterClient.agentsResearch(characters.getCharacters());
    }

    /**
     * 蓝图
     * */
    @Override
    public List<EvsiCharacterBlueprintsDTO> blueprints() {
        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);
        List<EvsiCharacterBlueprintsDTO> blueprintsDTOList = new ArrayList<>();
        for (int i=1; i<100; i++) {
            List<EvsiCharacterBlueprintsDTO> blueprints = characterClient.blueprints(characters.getCharacters(), i);
            blueprintsDTOList.addAll(blueprints);
            if (blueprints.size() < 1000) {
                break;
            }
        }
        blueprintsDTOList.forEach(v -> {
            EvsiInvtypes invtypes = invtypesService.getOne(new LambdaQueryWrapper<EvsiInvtypes>().eq(EvsiInvtypes::getTypeId, v.getType_id()));
            v.setType_name(invtypes.getTypeName());
        });
        return blueprintsDTOList;
    }

    /**
     * 获取军团历史
     * */
    @Override
    public List<EvsiCharacterCorporationHistoryDTO> corporationHistory() {
        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);
        List<EvsiCharacterCorporationHistoryDTO> corporationHistoryDTOS = characterClient.corporationHistory(characters.getCharacters());
        corporationHistoryDTOS.forEach(v -> {
            v.setCorporation_name(corporationClient.corporations(v.getCorporation_id()).getName());
        });
        return corporationHistoryDTOS;
    }

    /**
     * 跳跃疲劳
     * */
    @Override
    public EvsiCharacterFatigueDTO fatigue() {
        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);
        return characterClient.fatigue(characters.getCharacters());
    }

    /**
     * 奖牌
     * */
    @Override
    public Object medals() {
        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);
        return characterClient.medals(characters.getCharacters());
    }

    /**
     * 角色通知
     * */
    @Override
    public List<EvsiCharacterNotificationsDTO> notifications() {
        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);
        return characterClient.notifications(characters.getCharacters());
    }

    /**
     * 联系人通知
     * */
    @Override
    public List<EvsiCharacterNotificationContactsDTO> notificationContacts() {
        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);
        return characterClient.notificationContacts(characters.getCharacters());
    }

    /**
     * 角色肖像
     * */
    @Override
    public EvsiCharacterPortraitDTO portrait() {
        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);
        return characterClient.portrait(characters.getCharacters());
    }

    /**
     * 角色所在军团权限
     * */
    @Override
    public EvsiCharacterRolesDTO roles() {
        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);
        return characterClient.roles(characters.getCharacters());
    }

    /**
     * 角色榜单
     * */
    @Override
    public List<EvsiCharacterStandingsDTO> standings() {
        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);
        return characterClient.standings(characters.getCharacters());
    }

    /**
     * 军团标题
     * */
    @Override
    public Object titles() {
        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);
        return characterClient.titles(characters.getCharacters());
    }
}
